import { _decorator, Component, Label, Node } from 'cc';
import { UserInfo } from './UserInfo';
import { AudioMgr } from './utils/AudioMgr';
import { Start } from './Start';
import { Adsdk } from './utils/Adsdk';
const { ccclass, property } = _decorator;

@ccclass('ShopPanel')
export class ShopPanel extends Component {

    @property(Node)
    itemRoot: Node;
    @property(Label)
    titleLabel: Label;
    @property(Label)
    diamondNumLabel:Label
    p1Orp2 = 0;

    init(_p1Orp2) {
        this.p1Orp2 = _p1Orp2;
        this.diamondNumLabel.string=UserInfo.diamondNum+""

        if (this.p1Orp2 == 0) {//p1商店
            this.titleLabel.string="p1角色选择"
            for (let i = 0; i < 6; i++) {
                if (UserInfo.p1HasSkinArr.indexOf(i) == -1) {
                    this.itemRoot.children[i].getChildByName("advBtn").active = true;
                    this.itemRoot.children[i].getChildByName("buyBtn").active = true;
                    this.itemRoot.children[i].getChildByName("selectBtn").active = false;
                }
                else {
                    this.itemRoot.children[i].getChildByName("advBtn").active = false;
                    this.itemRoot.children[i].getChildByName("buyBtn").active = false;
                    this.itemRoot.children[i].getChildByName("selectBtn").active = true;

                    if (UserInfo.p1SkinSelect == i) {
                        this.itemRoot.children[i].getChildByName("selectBtn").getChildByName("Label").getComponent(Label).string = "已使用";
                    }
                    else {
                        this.itemRoot.children[i].getChildByName("selectBtn").getChildByName("Label").getComponent(Label).string = "使用";
                    }
                }

            }
        }
        else {//p2商店
            this.titleLabel.string="p2角色选择"

            for (let i = 0; i < 6; i++) {
                if (UserInfo.p2HasSkinArr.indexOf(i) == -1) {
                    this.itemRoot.children[i].getChildByName("advBtn").active = true;
                    this.itemRoot.children[i].getChildByName("buyBtn").active = true;
                    this.itemRoot.children[i].getChildByName("selectBtn").active = false;
                }
                else {
                    this.itemRoot.children[i].getChildByName("advBtn").active = false;
                    this.itemRoot.children[i].getChildByName("buyBtn").active = false;
                    this.itemRoot.children[i].getChildByName("selectBtn").active = true;

                    console.log(UserInfo.p2SkinSelect,"UserInfo.p2SkinSelect")
                    console.log(typeof(UserInfo.p2SkinSelect),"UserInfo.p2SkinSelecttype")
                    if (UserInfo.p2SkinSelect == i) {
                        this.itemRoot.children[i].getChildByName("selectBtn").getChildByName("Label").getComponent(Label).string = "已使用";
                    }
                    else {
                        this.itemRoot.children[i].getChildByName("selectBtn").getChildByName("Label").getComponent(Label).string = "使用";
                    }
                }

            }
        }

    }

    onclickBuyBtn(event, index) {
        AudioMgr.ins.playOneShot("audio/click", 0.8);
        if (UserInfo.diamondNum >= 4000) {
            UserInfo.diamondNum -= 4000;
            Start.ins.createWindowTip("购买成功")
            if (this.p1Orp2 == 0) {
                UserInfo.p1HasSkinArr.push(parseInt(index));
            }
            else {
                UserInfo.p2HasSkinArr.push(parseInt(index));

            }
            Start.ins.saveGameData();
        this.init(this.p1Orp2);
        }
        else {
            Start.ins.createWindowTip("钻石不足")
        }

    }

    onclickGetSkinSeeAdvBtn(event, index) {
        Adsdk.instance.showVideo(() => {
            if (this.p1Orp2 == 0) {
                UserInfo.p1HasSkinArr.push(parseInt(index));
            }
            else {
                UserInfo.p2HasSkinArr.push(parseInt(index));

            }
            Start.ins.saveGameData();
            this.init(this.p1Orp2);
        }, () => {

        })

    }

    onclickSelectBtn(event,index){
        AudioMgr.ins.playOneShot("audio/click", 0.8);
        if (this.p1Orp2 == 0) {
            UserInfo.p1SkinSelect=parseInt(index);
        }
        else {
            UserInfo.p2SkinSelect=parseInt(index);
        }
        Start.ins.saveGameData();
        this.init(this.p1Orp2);
        Start.ins.renewSelectSkinInHome();
    }

}


